package com.syqc.monitor.ui.historyline

import android.graphics.Color
import androidx.core.view.isVisible
import autodispose2.autoDispose
import com.syqc.comlib.UserConstant
import com.syqc.comlib.base.BaseBarActivity
import com.syqc.comlib.entity.MapMarker
import com.syqc.comlib.utils.ToolbarHelper
import com.syqc.comlib.utils.setResultTo
import com.syqc.maps.utils.MarkerSet
import com.syqc.monitor.R
import com.syqc.monitor.color.ColorsPopup
import com.syqc.monitor.databinding.ActivityMapMarkerBinding
import com.syqc.monitor.databinding.IncludeSelectSureBinding
import com.syqc.utils.AppConfigUtil
import com.syqc.utils.Keys

class MapMakerActivity :
    BaseBarActivity<ActivityMapMarkerBinding>(ActivityMapMarkerBinding::inflate) {
    private lateinit var mSureVB: IncludeSelectSureBinding
    override fun initView() {
        //重置未保存设置
        MarkerSet.reset()
        mSureVB = IncludeSelectSureBinding.bind(binding.root)
        mSureVB.tvSure.setText(R.string.desc_com_sure)
        ToolbarHelper.setToolbar(this, R.string.menu_map_marker)
        initSpeedPlate()
        setMarkColor(MarkerSet.localSet)
        initColorType(MarkerSet.localSet.colorType)

        MarkerSet.mapMarkerObserver().autoDispose(scopeProvider).subscribe {
            setMarkColor(it)
        }

        AppConfigUtil.saveLive.autoDispose(scopeProvider).subscribe {
            setResultTo(this)
            finish()
        }
    }

    private fun setMarkColor(it: MapMarker) {
        initTextSize(it.textSize)
        initImgSize(it.iconSize)
        val textColor = Color.parseColor(it.textColor)
        val backCarColor = Color.parseColor(it.backCarColor)
        val backSpeedColor = Color.parseColor(it.backSpeedColor)

        binding.tvMarkerPlat.setTextColor(textColor)
        binding.tvMarkerSpeed.setTextColor(textColor)

        binding.ivTextColor.setBackgroundColor(textColor)
        binding.tvMarkerPlat.setBackgroundColor(backCarColor)
        binding.ivPlateColor.setBackgroundColor(backCarColor)
        binding.ivSpeedColor.setBackgroundColor(backSpeedColor)
        binding.tvMarkerSpeed.setBackgroundColor(backSpeedColor)
    }

    private fun initSpeedPlate() {
        val showPlate = MarkerSet.localSet.showPlate
        val showSpeed = MarkerSet.localSet.showSpeed

        binding.rdbPlate.isChecked = showPlate
        binding.rdbName.isChecked = !showPlate

        binding.rdbTrue.isChecked = showSpeed
        binding.rdbFalse.isChecked = !showSpeed

        binding.tvMarkerSpeed.isVisible = showSpeed
        binding.llSpeedColor.isVisible = showSpeed

        if (showPlate) {
            binding.tvMarkerPlat.setText(R.string.desc_marker_plate)
        } else {
            binding.tvMarkerPlat.setText(R.string.desc_marker_car_name)

        }
    }


    /** 初始化字体大小 */
    private fun initTextSize(textSize: Int) {
        changeTextSize(textSize)
        when (textSize) {
            14 -> {
                binding.rdbSize1.isChecked = true
            }

            16 -> {
                binding.rdbSize2.isChecked = true
            }

            18 -> {
                binding.rdbSize3.isChecked = true
            }

            else -> {
                binding.rdbSize4.isChecked = true
            }
        }
    }

    /** 初始化字体大小 */
    private fun initImgSize(imgSize: Int) {
        changeImgSize(imgSize)
        when (imgSize) {
            60 -> {
                binding.rdbImg1.isChecked = true
            }

            70 -> {
                binding.rdbImg2.isChecked = true
            }

            80 -> {
                binding.rdbImg3.isChecked = true
            }

            else -> {
                binding.rdbImg4.isChecked = true
            }
        }
    }

    /** 样式 */
    private fun initColorType(type: Int) {
        when (type) {
            1 -> binding.rdbPlanOne.isChecked = true
            2 -> binding.rdbPlanTwo.isChecked = true
            3 -> binding.rdbPlanThree.isChecked = true
            else -> {
                binding.rdbPlanDefine.isChecked = true
                binding.llMarkerPlan.isVisible = true
            }
        }
    }

    private fun changeTextSize(textSize: Int) {
        val textSizeF = textSize.toFloat()
        binding.tvMarkerPlat.textSize = textSizeF
        binding.tvMarkerSpeed.textSize = textSizeF
    }

    private fun changeImgSize(imgSize: Int) {
        val layoutParams = binding.ivMarkerLogo.layoutParams
        layoutParams.width = imgSize
        layoutParams.height = imgSize
        binding.ivMarkerLogo.layoutParams = layoutParams

    }

    private var type: Int = 0
    private val mPopup by lazy { ColorsPopup(this, R.string.desc_color_plat, colorCallback) }
    private val colorCallback = object : ColorsPopup.Callback {
        override fun callback(index: Int, color: String) {
            val colorInt = Color.parseColor(color)
            when (type) {
                0 -> {
                    MarkerSet.setTextColor(color)
                    binding.tvMarkerPlat.setTextColor(colorInt)
                    binding.tvMarkerSpeed.setTextColor(colorInt)
                    binding.ivTextColor.setBackgroundColor(colorInt)
                }

                1 -> {
                    MarkerSet.setPlateColor(color)
                    binding.tvMarkerPlat.setBackgroundColor(colorInt)
                    binding.ivPlateColor.setBackgroundColor(colorInt)
                }

                else -> {
                    MarkerSet.setSpeedColor(color)
                    binding.tvMarkerSpeed.setBackgroundColor(colorInt)
                    binding.ivSpeedColor.setBackgroundColor(colorInt)
                }
            }
        }
    }

    private fun changeColor(color: String) {
        mPopup.showPopup(window.decorView, color)
    }


    override fun setListener() {
        binding.rdbPlate.setOnClickListener {
            binding.tvMarkerPlat.setText(R.string.desc_marker_plate)
            MarkerSet.showPlateName(true)
        }

        binding.rdbName.setOnClickListener {
            binding.tvMarkerPlat.setText(R.string.desc_marker_car_name)
            MarkerSet.showPlateName(false)
        }

        binding.rdbTrue.setOnClickListener {
            binding.tvMarkerSpeed.isVisible = true
            binding.llSpeedColor.isVisible = true
            MarkerSet.showSpeed(true)
        }

        binding.rdbFalse.setOnClickListener {
            binding.tvMarkerSpeed.isVisible = false
            binding.llSpeedColor.isVisible = false
            MarkerSet.showSpeed(false)
        }

        mSureVB.tvSure.setOnClickListener {
            MarkerSet.recycleLruCache()
            if (UserConstant.loginType == Keys.LOGIN_TYPE_CAR) {
                MarkerSet.saveMarkerSet()
                AppConfigUtil.saveCarSetCfg()
            } else {
                AppConfigUtil.saveConfig(4, MarkerSet.saveMarkerSet())
            }
        }

        binding.rdbPlanOne.setOnClickListener {
            binding.llMarkerPlan.isVisible = false
            MarkerSet.changeColorType(1)
        }

        binding.rdbPlanTwo.setOnClickListener {
            binding.llMarkerPlan.isVisible = false
            MarkerSet.changeColorType(2)
        }

        binding.rdbPlanThree.setOnClickListener {
            binding.llMarkerPlan.isVisible = false
            MarkerSet.changeColorType(3)
        }

        binding.rdbPlanDefine.setOnClickListener {
            binding.llMarkerPlan.isVisible = true
            MarkerSet.changeColorType(0)
        }

        binding.ivTextColor.setOnClickListener {
            if (binding.rdbPlanDefine.isChecked) {
                type = 0
                changeColor(MarkerSet.localSet.textColor)
            }
        }

        binding.ivPlateColor.setOnClickListener {
            if (binding.rdbPlanDefine.isChecked) {
                type = 1
                changeColor(MarkerSet.localSet.backCarColor)
            }
        }

        binding.ivSpeedColor.setOnClickListener {
            if (binding.rdbPlanDefine.isChecked) {
                type = 2
                changeColor(MarkerSet.localSet.backSpeedColor)
            }
        }

        binding.rbgTextSize.setOnCheckedChangeListener { _, checkedId ->
            when (checkedId) {
                R.id.rdbSize1 -> {
                    MarkerSet.setTextSize(14)
                }

                R.id.rdbSize2 -> {
                    MarkerSet.setTextSize(16)
                }

                R.id.rdbSize3 -> {
                    MarkerSet.setTextSize(18)
                }

                R.id.rdbSize4 -> {
                    MarkerSet.setTextSize(20)
                }
            }
            changeTextSize(MarkerSet.localSet.textSize)
        }

        binding.rbgImgSize.setOnCheckedChangeListener { _, i ->
            when (i) {
                R.id.rdbImg1 -> {
                    MarkerSet.setIconSize(60)
                }

                R.id.rdbImg2 -> {
                    MarkerSet.setIconSize(70)
                }

                R.id.rdbImg3 -> {
                    MarkerSet.setIconSize(80)
                }

                R.id.rdbImg4 -> {
                    MarkerSet.setIconSize(90)
                }
            }
            changeImgSize(MarkerSet.localSet.iconSize)
        }
    }

}